.TH std::basic_string::compare 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::basic_string::compare \- std::basic_string::compare

.SH Synopsis
                                                      (noexcept since
   int compare( const basic_string& str ) const;  \fB(1)\fP C++11)
                                                      (constexpr since
                                                      C++20)
   int compare( size_type pos1, size_type count1, \fB(2)\fP (constexpr since
                const basic_string& str ) const;      C++20)
   int compare( size_type pos1, size_type count1,

                const basic_string& str,                               \fI(until C++14)\fP

                size_type pos2, size_type count2
   ) const;
   int compare( size_type pos1, size_type count1,
                                                                       \fI(since C++14)\fP
                const basic_string& str,                               (constexpr since
                                                                       C++20)
                size_type pos2, size_type count2
   = npos ) const;
   int compare( const CharT* s ) const;               \fB(4)\fP              (constexpr since
                                                                       C++20)
   int compare( size_type pos1, size_type count1,     \fB(5)\fP              (constexpr since
                const CharT* s ) const;                                C++20)
   int compare( size_type pos1, size_type count1, \fB(3)\fP                  (constexpr since
                const CharT* s, size_type count2      \fB(6)\fP              C++20)
   ) const;
   template< class StringViewLike >                                    \fI(since C++17)\fP
   int compare( const StringViewLike& t ) const       \fB(7)\fP              (constexpr since
   noexcept(/* see below */);                                          C++20)
   template< class StringViewLike >
                                                                       \fI(since C++17)\fP
   int compare( size_type pos1, size_type count1,     \fB(8)\fP              (constexpr since
                                                                       C++20)
                const StringViewLike& t ) const;
   template< class StringViewLike >

   int compare( size_type pos1, size_type count1,                      \fI(since C++17)\fP
                const StringViewLike& t,              \fB(9)\fP              (constexpr since
                                                                       C++20)
                size_type pos2, size_type count2
   = npos) const;

   Compares two character sequences.

   1) Compares this string to str.
   2) Compares a [pos1, pos1 + count1) substring of this string to str.
     * If count1 > size() - pos1, the substring is [pos1, size()).
   3) Compares a [pos1, pos1 + count1) substring of this string to a substring
   [pos2, pos2 + count2) of str.
     * If count1 > size() - pos1, the first substring is [pos1, size()).
     * If count2 > str.size() - pos2, the second substring is [pos2, str.size()).
   4) Compares this string to the null-terminated character sequence beginning at the
   character pointed to by s with length Traits::length(s).
   5) Compares a [pos1, pos1 + count1) substring of this string to the null-terminated
   character sequence beginning at the character pointed to by s with length
   Traits::length(s).
     * If count1 > size() - pos1, the substring is [pos1, size()).
   6) Compares a [pos1, pos1 + count1) substring of this string to the characters in
   the range [s, s + count2). The characters in [s, s + count2) may include null
   characters.
     * If count1 > size() - pos1, the substring is [pos1, size()).
   7-9) Implicitly converts t to a string view sv as if by
   std::basic_string_view<CharT, Traits> sv = t;, then
   7) compares this string to sv;
   8) compares a [pos1, pos1 + count1) substring of this string to sv, as if by
   std::basic_string_view<CharT, Traits>(*this).substr(pos1, count1).compare(sv);
   9) compares a [pos1, pos1 + count1) substring of this string to a substring
   [pos2, pos2 + count2) of sv, as if by std::basic_string_view<CharT, Traits>(*this)
       .substr(pos1, count1).compare(sv.substr(pos2, count2)).
   These overloads participate in overload resolution only if
   std::is_convertible_v<const StringViewLike&,
                         std::basic_string_view<CharT, Traits>> is true and
   std::is_convertible_v<const StringViewLike&, const CharT*> is false..

   A character sequence consisting of count1 characters starting at data1 is compared
   to a character sequence consisting of count2 characters starting at data2 as
   follows:

     * First, calculate the number of characters to compare, as if by size_type rlen =
       std::min(count1, count2).
     * Then compare the sequences by calling Traits::compare(data1, data2, rlen). For
       standard strings this function performs character-by-character lexicographical
       comparison. If the result is zero (the character sequences are equal so far),
       then their sizes are compared as follows:

                        Condition                             Result       Return value
   Traits::compare(data1, data2, rlen) < 0              data1 is less than <0
                                                        data2
                                          size1 < size2 data1 is less than <0
                                                        data2
   Traits::compare(data1, data2, rlen) == size1 ==      data1 is equal to  0
   0                                      size2         data2
                                          size1 > size2 data1 is greater   >0
                                                        than data2
   Traits::compare(data1, data2, rlen) > 0              data1 is greater   >0
                                                        than data2

.SH Parameters

   str    - other string to compare to
   s      - pointer to the character string to compare to
   count1 - number of characters of this string to compare
   pos1   - position of the first character in this string to compare
   count2 - number of characters of the given string to compare
   pos2   - position of the first character of the given string to compare
   t      - object (convertible to std::basic_string_view) to compare to

.SH Return value

     * Negative value if *this appears before the character sequence specified by the
       arguments, in lexicographical order.
     * Zero if both character sequences compare equivalent.
     * Positive value if *this appears after the character sequence specified by the
       arguments, in lexicographical order.

.SH Exceptions

   The overloads taking parameters named pos1 or pos2 throws std::out_of_range if the
   argument is out of range.

   7)
   noexcept specification:
   noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT,
   Traits>>)
   8,9) Throws anything thrown by the conversion to std::basic_string_view.

   If an exception is thrown for any reason, this function has no effect (strong
   exception safety guarantee).

.SH Possible implementation

                                      overload \fB(1)\fP
   template<class CharT, class Traits, class Alloc>
   int std::basic_string<CharT, Traits, Alloc>::compare
       (const std::basic_string& s) const noexcept
   {
       size_type lhs_sz = size();
       size_type rhs_sz = s.size();
       int result = traits_type::compare(data(), s.data(), std::min(lhs_sz, rhs_sz));
       if (result != 0)
           return result;
       if (lhs_sz < rhs_sz)
           return -1;
       if (lhs_sz > rhs_sz)
           return 1;
       return 0;
   }

.SH Notes

   For the situations when three-way comparison is not required, std::basic_string
   provides the usual relational operators (<, <=, ==, >, etc).

   By default (with the default std::char_traits), this function is not
   locale-sensitive. See std::collate::compare for locale-aware three-way string
   comparison.

.SH Example


// Run this code

 #include <cassert>
 #include <iomanip>
 #include <iostream>
 #include <string>
 #include <string_view>

 void print_compare_result(std::string_view str1,
                           std::string_view str2,
                           int compare_result)
 {
     if (compare_result < 0)
         std::cout << std::quoted(str1) << " comes before "
                   << std::quoted(str2) << ".\\n";
     else if (compare_result > 0)
         std::cout << std::quoted(str2) << " comes before "
                   << std::quoted(str1) << ".\\n";
     else
         std::cout << std::quoted(str1) << " and "
                   << std::quoted(str2) << " are the same.\\n";
 }

 int main()
 {
     std::string batman{"Batman"};
     std::string superman{"Superman"};
     int compare_result{0};

     // 1) Compare with other string
     compare_result = batman.compare(superman);
     std::cout << "1) ";
     print_compare_result("Batman", "Superman", compare_result);

     // 2) Compare substring with other string
     compare_result = batman.compare(3, 3, superman);
     std::cout << "2) ";
     print_compare_result("man", "Superman", compare_result);

     // 3) Compare substring with other substring
     compare_result = batman.compare(3, 3, superman, 5, 3);
     std::cout << "3) ";
     print_compare_result("man", "man", compare_result);

     // Compare substring with other substring
     // defaulting to end of other string
     assert(compare_result == batman.compare(3, 3, superman, 5));

     // 4) Compare with char pointer
     compare_result = batman.compare("Superman");
     std::cout << "4) ";
     print_compare_result("Batman", "Superman", compare_result);

     // 5) Compare substring with char pointer
     compare_result = batman.compare(3, 3, "Superman");
     std::cout << "5) ";
     print_compare_result("man", "Superman", compare_result);

     // 6) Compare substring with char pointer substring
     compare_result = batman.compare(0, 3, "Superman", 5);
     std::cout << "6) ";
     print_compare_result("Bat", "Super", compare_result);
 }

.SH Output:

 1) "Batman" comes before "Superman".
 2) "Superman" comes before "man".
 3) "man" and "man" are the same.
 4) "Batman" comes before "Superman".
 5) "Superman" comes before "man".
 6) "Bat" comes before "Super".

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

      DR    Applied to       Behavior as published              Correct behavior
                       the parameter count2 of overload   default argument removed,
   LWG 5    C++98      \fB(6)\fP                                split to overloads \fB(5)\fP and
                       had a default argument npos        \fB(6)\fP
   LWG 847  C++98      there was no exception safety      added strong exception safety
                       guarantee                          guarantee
   LWG 2946 C++17      overload \fB(7)\fP caused ambiguity in   avoided by making it a
                       some cases                         template
                       noexcept for overload \fB(7)\fP was
   P1148R0  C++17      accidentally                       restored
                       dropped by the resolution of
                       LWG2946

.SH See also

   operator==
   operator!=
   operator<
   operator>
   operator<=
   operator>=              lexicographically compares two strings
   operator<=>             \fI(function template)\fP
   (removed in C++20)
   (removed in C++20)
   (removed in C++20)
   (removed in C++20)
   (removed in C++20)
   (C++20)
   substr                  returns a substring
                           \fI(public member function)\fP
   collate                 defines lexicographical comparison and hashing of strings
                           \fI(class template)\fP
   strcoll                 compares two strings in accordance to the current locale
                           \fI(function)\fP
                           returns true if one range is lexicographically less than
   lexicographical_compare another
                           \fI(function template)\fP
                           compares two views
   compare                 \fI\fI(public member\fP function of\fP
                           std::basic_string_view<CharT,Traits>)

.SH Category:
     * conditionally noexcept
